package com.wx.dao;

import com.wx.pojo.CheckGroup;
import com.wx.pojo.CheckItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CheckGroupDao {
    @Select("select * from t_checkgroup where code=#{query} or name like '%${query}%' or helpCode like '%${query}%'")
    List<CheckGroup> findAllGroup(@Param("query") String query);

    /**
     * 依据id查询某个组
     * @param id
     * @return
     */
    @Select("select * from t_checkgroup where id=#{id} ")
    CheckGroup findGroupById(Integer id);

    /**
     * 添加检查组
     * @param checkGroup
     */
    @Insert("insert into t_checkgroup (code,name,helpCode,sex,remark,attention) values(#{code},#{name},#{helpCode},#{sex},#{remark},#{attention}) ")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void addGroup(CheckGroup checkGroup);

    /**
     * 删除检查组
     * @param id
     */
    @Delete("delete from t_checkgroup where id=#{id} ")
    void deleteGroupById(Integer id);

    /**
     * 修改检查组
     * @param checkGroup
     */

    /**
     * 添加中间表数据
     */
    @Insert("insert into t_checkgroup_checkitem values(#{groupId},#{itermId})")
    void addCheckgroupCheckitem(@Param("groupId") Integer groupId,@Param("itermId") Integer itermId);
    /**
     * 删除中间表中的信息
     */
    @Delete("delete  from t_checkgroup_checkitem where checkgroup_id=#{id} ")
    void deleteCheckgroupCheckitem(Integer id);

    /**
     * 依据检查组的查询出对应的所有检查项
     * @param id
     * @return
     */
    //  @Select("select * from t_checkitem where id=#{id}")
    //    CheckItem findChecitemById(Integer id);
    @Select("SELECT * FROM `t_checkgroup` WHERE id =#{id}")
    @Results({
            @Result(
                    property = "checkItems",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "finaAllItem")
            )
    })
     CheckGroup findItemsById(Integer id);
    @Select("SELECT * FROM `t_checkitem` tc INNER JOIN `t_checkgroup_checkitem` tcg ON tc.id=tcg.checkitem_id WHERE checkgroup_id=#{id}")
    List<CheckItem> finaAllItem(Integer id);

    /**
     * 删除所有中间表数据然后添加
     */
    @Delete("delete from t_checkgroup_checkitem where checkgroup_id=#{gid} ")
    void deleteAllMidtable(Integer gid);


}
